import 'package:flutter/material.dart';
import 'package:flutter/services.dart';

// import 'package:flutter/cupertino.dart';
import 'package:flutter_yn_goods/config/app_color.dart';
import 'package:flutter_yn_goods/config/app_routes.dart';
import 'package:flutter_yn_goods/generated/assets.dart';
import 'package:flutter_yn_goods/main.dart';
import 'package:flutter_yn_goods/widgets/image/auto_image.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';

class LoginInputItemView extends StatelessWidget {
  const LoginInputItemView({
    super.key,
    required this.title,
    required this.textEditingController,
    this.maxLength,
    this.rightChild,
    this.rightText = "",
    this.errorText = "",
    this.hintText = "",
    this.textChanged,
    this.obscureText = false,
    this.focusNode,
    this.isRequired = false,
    this.keyboardType,
    this.margin,
  });

  final String title;
  final TextEditingController textEditingController;
  final int? maxLength;
  final Widget? rightChild;
  final String rightText;
  final String errorText;
  final String hintText;
  final ValueChanged<String>? textChanged;
  final bool obscureText;
  final FocusNode? focusNode;
  final bool isRequired;
  final TextInputType? keyboardType;
  final EdgeInsetsGeometry? margin;

  @override
  Widget build(BuildContext context) {
    return Container(
      margin: margin ?? EdgeInsets.symmetric(horizontal: 24.w),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          Row(
            children: [
              Text(
                title,
                style: TextStyle(
                    color: AppColor.color282828,
                    fontSize: 12.sp,
                    fontWeight: FontWeight.w600),
              ),
              4.horizontalSpace,
              Visibility(
                  visible: isRequired,
                  child: Text(
                    '*',
                    style: TextStyle(
                        color: AppColor.primary,
                        fontSize: 12.sp,
                        fontWeight: FontWeight.w600),
                  ))
            ],
          ),
          12.verticalSpaceFromWidth,
          _buildAccountInputWidget(
            controller: textEditingController,
            hintText: hintText,
            obscureText: obscureText,
            rightChild: rightChild,
            maxLength: maxLength,
            rightText: rightText,
            textChanged: textChanged,
            focusNode: focusNode,
          ),
          if (errorText.isNotEmpty) ...[
            4.verticalSpaceFromWidth,
            Row(
              children: [
                AutoImage(
                  Assets.alertError,
                  width: 20.w,
                  height: 20.w,
                ),
                8.horizontalSpace,
                Text(
                  errorText,
                  style: TextStyle(
                      color: AppColor.colorE64A4A,
                      fontSize: 12.sp,
                      fontWeight: FontWeight.w400),
                ),
              ],
            )
          ],
        ],
      ),
    );
  }

  Widget _buildAccountInputWidget({
    required TextEditingController controller,
    String hintText = "",
    bool obscureText = false,
    Widget? rightChild,
    int? maxLength,
    String rightText = "",
    ValueChanged<String>? textChanged,
    FocusNode? focusNode,
  }) {
    return Container(
      height: 52.w,
      decoration: BoxDecoration(
        color: AppColor.colorF5F5F5,
        borderRadius: BorderRadius.circular(12.w),
      ),
      alignment: Alignment.centerLeft,
      padding: EdgeInsets.symmetric(horizontal: 16.w),
      child: Row(
        children: [
          Expanded(
            child: TextField(
              focusNode: focusNode,
              controller: controller,
              obscureText: obscureText,
              style: TextStyle(
                  color: AppColor.color282828,
                  fontSize: 14.sp,
                  fontWeight: FontWeight.w500),
              maxLength: maxLength,
              inputFormatters:
                  const TextInputType.numberWithOptions() == keyboardType
                      ? [FilteringTextInputFormatter.digitsOnly]
                      : [],
              keyboardType: keyboardType,
              decoration: InputDecoration(
                contentPadding: const EdgeInsets.all(0),
                border: noneBorder,
                focusedBorder: noneBorder,
                enabledBorder: noneBorder,
                hintStyle: TextStyle(
                    color: AppColor.color999999,
                    fontSize: 14.sp,
                    fontWeight: FontWeight.w400),
                hintText: hintText,
                counterText: "",
                isDense: true,
              ),
              onChanged: (text) {
                textChanged?.call(text);
              },
            ),
          ),
          if (rightChild != null) ...[
            rightChild,
          ],
          if (rightText.isNotEmpty) ...[
            Padding(
              padding: EdgeInsets.only(left: 4.w),
              child: Text(
                rightText ?? "",
                style: TextStyle(
                    color: AppColor.color999999,
                    fontSize: 10.sp,
                    fontWeight: FontWeight.w400),
              ),
            ),
          ],
        ],
      ),
    );
  }

  InputBorder get noneBorder {
    return const OutlineInputBorder(
      borderSide: BorderSide.none,
    );
  }
}

class PasswordShowOrHideSwitch extends StatelessWidget {
  const PasswordShowOrHideSwitch({super.key, required this.show, this.onTap});

  final bool show;
  final GestureTapCallback? onTap;

  @override
  Widget build(BuildContext context) {
    return InkWell(
      onTap: onTap,
      child: AutoImage(
        show ? Assets.eyeOpen : Assets.eyeClose,
        width: 20.w,
        height: 20.w,
      ),
    );
  }
}
